//
//  HomeCargoinLongDetaViewController.m
//  shuiyun
//
//  Created by 马田成 on 2017/9/14.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeCargoinLongDetaViewController.h"
#import "HomeCarLogDetaTableViewCell.h"
#import "HomePPPLLLTableViewCell.h"
#import "homeGoLodingViewController.h"

#import "HomeHistoruFBViewController.h"
#import "HomeSureZhuangViewController.h"//确认去装
#import "HomeFaBuPingLunViewController.h"//发布评论
@interface HomeCargoinLongDetaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableDictionary *dataDit;

@end

@implementation HomeCargoinLongDetaViewController

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self creationData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    [self regeditCell];
 //  [self creationData];
    
    [self setTitle:@"货源信息详情"];
    
    
    
    
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 50);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    self.tableView.estimatedRowHeight = 800;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    UIButton *buttonOfGo = [[UIButton alloc] init];
    [self.view addSubview:buttonOfGo];
    [buttonOfGo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.width.offset(DEF_SCREEN_WIDTH / 2);
        make.height.offset(50);
        make.bottom.equalTo(self.view);
        
    }];
    buttonOfGo.backgroundColor = [UIColor whiteColor];
    [buttonOfGo setTitle:@"去装记录" forState:UIControlStateNormal];
    [buttonOfGo setTitleColor:getColor(@"3F87F6") forState:UIControlStateNormal];
    
    [buttonOfGo addTarget:self action:@selector(goClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *buttonOfSure = [[UIButton alloc] init];
    [self.view addSubview:buttonOfSure];
    [buttonOfSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.width.offset(DEF_SCREEN_WIDTH / 2);
        make.height.offset(50);
        make.bottom.equalTo(self.view);
        
    }];
    buttonOfSure.backgroundColor = getColor(@"3F87F6");
    [buttonOfSure setTitle:@"确认去装" forState:UIControlStateNormal];
    [buttonOfSure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonOfSure addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeCarLogDetaTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeCarLogDetaTableViewCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"HomePPPLLLTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomePPPLLLTableViewCell"];
    
    
}
- (void)creationData{
    
    self.dataDit = [NSMutableDictionary dictionary];
    self.dataArr = [NSMutableArray array];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *userid = [PublicFunction getConfigWithKey:@"userid"];
    
    [productParams setValue:userid forKey:@"userid"];
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:_detailID forKey:@"id"];
    
    [[MNetworkHandler sharedInstance]conURL:hometGoodsDetails networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            _dataDit = [returnData objectForKey:@"data"];
            self.dataArr = [_dataDit objectForKey:@"Comment"];
 
            [self.view addSubview:self.tableView];
            NSLog(@"MMMMMMMMMM%@", _dataDit);
            NSLog(@"MMMMMMMMMM%@", self.dataArr);
            [self.tableView reloadData];
            
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];
    }];
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 64; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
    
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return 1;
    }else {
    
        return self.dataArr.count;
        
    }
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 560;
    }else {
    
        
        return UITableViewAutomaticDimension;
    }
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        HomeCarLogDetaTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomeCarLogDetaTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_dataDit) {
            NSString *str = @"发布时间";
            cell.s_time.text =[str stringByAppendingString:[_dataDit objectForKey:@"s_time"]] ;
            cell.name.text = [_dataDit objectForKey:@"name"];
            cell.tonnage.text = [_dataDit objectForKey:@"tonnage"];
            cell.start.text = [_dataDit objectForKey:@"start"];
            cell.end.text = [_dataDit objectForKey:@"end"];
            cell.package.text = [_dataDit objectForKey:@"package"];
            cell.shipping.text = [_dataDit objectForKey:@"shipping"];
            cell.term.text = [_dataDit objectForKey:@"term"];
            cell.valid.text = [_dataDit objectForKey:@"valid"];
            cell.linkman.text = [_dataDit objectForKey:@"linkman"];
            cell.phone.text = [_dataDit objectForKey:@"phone"];
            cell.zhima.text = [_dataDit objectForKey:@"zhima"];
            [cell.historyFaBu addTarget:self action:@selector(historyFaBuClick) forControlEvents:UIControlEventTouchUpInside];
            [cell.pingLunFB addTarget:self action:@selector(pingLunFBClick) forControlEvents:UIControlEventTouchUpInside];
            
        }
        return cell;
    }else {
    
        HomePPPLLLTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomePPPLLLTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSMutableDictionary *dic = self.dataArr[indexPath.row];
        if (dic) {
            cell.saytext.text = [[[dic objectForKey:@"username"] stringByAppendingString:@":"] stringByAppendingString:[dic objectForKey:@"saytext"]];
            
            
        }
        
        return cell;
    }
    

    
    
    
}

//去装记录
- (void)goClick{
    homeGoLodingViewController *vc = [[homeGoLodingViewController alloc] init];
    vc.goID = [self.dataDit objectForKey:@"id"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
//发布历史
- (void)historyFaBuClick{

    HomeHistoruFBViewController *vc= [[HomeHistoruFBViewController alloc] init];
    
    
    vc.huaJiaID =[self.dataDit objectForKey:@"id"];

    [self.navigationController pushViewController:vc animated:YES];
    

    
}
//确认去装
- (void)sureClick{

    HomeSureZhuangViewController *vc = [[HomeSureZhuangViewController alloc] init];
    vc.huaJiaID =[self.dataDit objectForKey:@"id"];
    vc.dunNum = [_dataDit objectForKey:@"tonnage"];
    [self.navigationController pushViewController:vc animated:YES];
    

    

}
//发布评论
- (void)pingLunFBClick{

    HomeFaBuPingLunViewController *vc = [[HomeFaBuPingLunViewController alloc] init];
    vc.huaJiaID =[self.dataDit objectForKey:@"id"];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
